﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Common.Entity;
using ESOL.Business.Prod;
using ESOL.Business.Sales;
using ESOL.Business.Mst;
using ESOL.Common;
using ESOL.Business.Proj;
using ESOL.WinUI.Manufacture;

namespace ESOL.WinUI.Proj
{
    public partial class frmManageProductPlanOrder : BaseForm.frmBaseMdiBusinessForm
    {
        #region vars
        private ProdPlanOrderEntity mSelectedProductPlanOrder;
        private MstProductEntity mSelectedProduct;

        #endregion

        #region ctors
        public frmManageProductPlanOrder()
        {
            InitializeComponent();
        }

        #endregion

        #region functions
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbProductPlanOrderStatus, "PSTATUS", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colStatus, "PSTATUS", true);

            //UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colOrderItemType, "POIT", false);
            //UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colStatus, "PSTATUS", false);//生产类型
            //UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colItemProductType, "PDTYPE", false);//生产类型
            //UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colItemStatus, "POI", false);//计划单明细状态
            //UICommon.UICommonFunctions.BindGridComboboxByEnum(colIsMark, typeof(BoolStatus), typeof(int), true);
            //UICommon.UICommonFunctions.BindGridComboboxByEnum(colItemIsMark, typeof(BoolStatus), typeof(int), true);

        }

        private string GetCondition()
        {
            string sCondition = "";
            if (string.IsNullOrEmpty(txtContractModel.Text) == false)
            {
                sCondition += "and sppai.PRODUCT_SPEC_OUT like '%" + txtContractModel.Text + "%' ";
            }
            if (string.IsNullOrEmpty(txtProductCode.Text) == false)
            {
                sCondition += "and mp.PRODUCT_CODE like '%" + txtProductCode.Text + "%' ";
            }
            if (string.IsNullOrEmpty(cmbProductPlanOrderStatus.SelectedValue.ToString()) == false)
            {
                sCondition += "and ppo.STATUS='" + cmbProductPlanOrderStatus.SelectedValue.ToString() + "' ";
            }
            else
            {
                sCondition += "and ppo.STATUS not in ('PSTATUS04','PSTATUS05','PSTATUS07') ";
            }
            if (string.IsNullOrEmpty(txtProductPlanApplication.Text) == false)
            {
                sCondition += "and sppa.APPLICATION_CODE like '%" + txtProductPlanApplication.Text + "%' ";
            }
            if (string.IsNullOrEmpty(txtProductPlanOrder.Text) == false)
            {
                sCondition += "and ppo.PLAN_ORDER_CODE like '%" + txtProductPlanOrder.Text + "%' ";
            }
            return sCondition;
        }

        private void BindGridData()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = " PPO.PLAN_ORDER_CODE DESC";
            DataTable dt = ProdPlanOrderBs.GetProdPlanOrderByCondition(condition, orderBy, rowCount);
            dt.Columns.Add("IS_SELECTED", typeof(Boolean));
            foreach (DataRow dr in dt.Rows)
            {
                dr["IS_SELECTED"] = false;
            }
            gvMain.DataSource = dt;
        }

        private void SetButton()
        {
            switch (mSelectedProductPlanOrder.Status)
            {
                case "PSTATUS01"://新建
                    btnModify.Enabled = true;
                    btnSplitOrder.Enabled = true;
                    btnMakeOrder.Enabled = true;
                    break;
                case "PSTATUS02"://外购
                    btnModify.Enabled = true;
                    btnSplitOrder.Enabled = true;
                    btnMakeOrder.Enabled = true;
                    break;
                case "PSTATUS03"://外购中
                    btnModify.Enabled = false;
                    btnSplitOrder.Enabled = false;
                    btnMakeOrder.Enabled = false;
                    break;
                case "PSTATUS06"://生产中
                    btnModify.Enabled = false;
                    btnSplitOrder.Enabled = false;
                    btnMakeOrder.Enabled = false;
                    break;
                case "PSTATUS05"://已关闭
                    btnModify.Enabled = false;
                    btnSplitOrder.Enabled = false;
                    btnMakeOrder.Enabled = false;
                    break;
                case "PSTATUS07"://已完成
                    btnModify.Enabled = false;
                    btnSplitOrder.Enabled = false;
                    btnMakeOrder.Enabled = false;
                    break;
            }
        }

        #endregion

        #region events
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            BindComboBox();
            BindGridData();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.RowHandle < 0)
                return;
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedProductPlanOrder = ProdPlanOrderBs.GetProdPlanOrderByID(dr["PLAN_ORDER_ID"].ToString());
            //mSelectedProductPlanOrder = new ProdPlanOrderEntity(gridView1.GetDataRow(e.RowHandle));
            SalesProdPlanApplicationItemEntity mPlanApplicationItem = SalesProdPlanApplicationItemBs.GetProdPlanApplicationItemByID(mSelectedProductPlanOrder.ApplicationItemId);
            if (mPlanApplicationItem == null)
            {
                MessageBox.Show("生产计划申请单不存在，请联系管理员！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            mSelectedProduct = MstProductBs.GetMstProductTypeByID(mPlanApplicationItem.ProductId);
            if (mSelectedProduct == null)
            {
                MessageBox.Show("产品编码不存在，请联系管理员！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            SetButton();
        }

        protected override void Click_View()
        {
            if (mSelectedProductPlanOrder == null)
            {
                MessageBox.Show("请选择一条生产计划单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            } 
            frmMaintainProductPlanOrder frm = new frmMaintainProductPlanOrder(mSelectedProductPlanOrder);
            frm.ThisActionType = ActionType.View;
            frm.ShowDialog();
        }
        protected override void Click_New()
        {

            frmMaintainProductPlanOrder frm = new frmMaintainProductPlanOrder();
            frm.ThisActionType = ActionType.New;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                BindGridData();
            }
        }
        protected override void Click_Modify()
        {
            if (mSelectedProductPlanOrder == null)
            {
                MessageBox.Show("请选择一条生产计划单！","Information",MessageBoxButtons.OK,MessageBoxIcon.Information);
                return;
            }
            frmMaintainProductPlanOrder frm = new frmMaintainProductPlanOrder(mSelectedProductPlanOrder);
            if (frm.ShowDialog() == DialogResult.OK)
            {
                BindGridData();
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            mSelectedProductPlanOrder = null;
            BindGridData();
        }

        private void btnMBOM_Click(object sender, EventArgs e)
        {
            if (mSelectedProduct == null)
            {
                MessageBox.Show("产品编码不存在，请联系管理员！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmManageMBOM frm = new frmManageMBOM();
            frm.SelectedProduct = mSelectedProduct;
            frm.SelectedPlanOrder = mSelectedProductPlanOrder;
            frm.MdiParent = this.MdiParent;
            frm.Show();
        }

        private void btnSplitOrder_Click(object sender, EventArgs e)
        {
            if (mSelectedProductPlanOrder == null)
            {
                MessageBox.Show("请选择一条生产计划单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainProductPlanOrderSplit frm = new frmMaintainProductPlanOrderSplit(mSelectedProductPlanOrder);
            if (frm.ShowDialog() == DialogResult.OK)
            {
                BindGridData();
            }
        }

        private void btnMakeOrder_Click(object sender, EventArgs e)
        {
            if (mSelectedProduct == null)
                return;
            if (ProjMBOMBs.IsExistedBom(mSelectedProduct.ProductId) == false)
            {
                MessageBox.Show("所选产品编码尚未建立物料清单，无法生单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainProductPlanOrderCreate frm = new frmMaintainProductPlanOrderCreate(mSelectedProductPlanOrder);
            if (frm.ShowDialog() == DialogResult.OK)
            {
                BindGridData();
            }
        }

        private void btnRemakeOrder_Click(object sender, EventArgs e)
        {
            if (mSelectedProductPlanOrder == null)
            {
                MessageBox.Show("请选择一张生产计划单！","Information",MessageBoxButtons.OK,MessageBoxIcon.Information);
                return;
            }
            if (!mSelectedProductPlanOrder.Status.Equals("PSTATUS05")
                && !mSelectedProductPlanOrder.Status.Equals("PSTATUS07"))
            {
                frmMaintainProductPlanOrderRecreate frm = new frmMaintainProductPlanOrderRecreate(mSelectedProductPlanOrder);
                frm.ThisActionType = ActionType.New;
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    BindGridData();
                }
            }
        }

        private void btnPurchase_Click(object sender, EventArgs e)
        {
            if (mSelectedProductPlanOrder == null)
            {
                MessageBox.Show("请选择一张生产计划单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainProductPlanOrderPurchase frm = new frmMaintainProductPlanOrderPurchase();
            frm.ThisActionType = ActionType.New;
            frm.mSelectedPlanOrder = mSelectedProductPlanOrder;
            frm.ShowDialog();
        }

        protected override void Click_Print()
        {
            base.Click_Print();
        }

        protected override void Click_Export()
        {
            SaveFileDialog frm = new SaveFileDialog();
            frm.Filter = "All Files (*.*)|";
            frm.DefaultExt = "xls";
            frm.AddExtension = true;
            frm.FileName = "生产计划单导出列表" + DateTime.Today.ToString("yyyy-MM-dd");
            if (frm.ShowDialog() == DialogResult.OK)
            {
                gvMain.ExportToXls(frm.FileName);
            }
        }

        private void btnReport_Click(object sender, EventArgs e)
        {
            frmPrintFormProductionReport frm = new frmPrintFormProductionReport();
            frm.ShowDialog();
        }

        private void btnPurImport_Click(object sender, EventArgs e)
        {
            DataTable dt = gvMain.DataSource as DataTable;
            List<ProdPlanOrderEntity> mlist = new List<ProdPlanOrderEntity>();

            foreach (DataRow mdr in dt.Rows)
            {
                if (Convert.ToBoolean(mdr["IS_SELECTED"].ToString()))
                {
                    ProdPlanOrderEntity entity = new ProdPlanOrderEntity(mdr);
                    mlist.Add(entity);
                }
            }
            if (mlist.Count == 0)
            {
                MessageBox.Show("请至少选择一个生产计划单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            foreach (ProdPlanOrderEntity entity in mlist)
            {
                if (entity.Status != "PSTATUS03")
                {
                    MessageBox.Show("所选生产计划单不为外购中！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }


            frmMaintainProdPurImportItems frm = new frmMaintainProdPurImportItems();
            frm.mSelectedProdPlanOrders = mlist;
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                BindGridData();
            }
        }

        private void repositoryItemHyperLinkEdit1_Click(object sender, EventArgs e)
        {
            DataRow dr = gridView1.GetFocusedDataRow();
            mSelectedProductPlanOrder = new ProdPlanOrderEntity(dr);
            if (mSelectedProductPlanOrder == null)
            {
                return;
            }
            frmManageManufactureOrder frm = new frmManageManufactureOrder(mSelectedProductPlanOrder);
            frm.MdiParent = this.MdiParent;
            frm.Show();
        }


        private void repositoryItemHyperLinkEdit2_Click(object sender, EventArgs e)
        {
            try
            {
                DevExpress.XtraEditors.HyperLinkEdit link = (DevExpress.XtraEditors.HyperLinkEdit)sender;
                string sProductCode = link.Text;
                EsolWevServices.EsolService ws = new EsolWevServices.EsolService();
                byte[] arrFileData = ws.DownloadFile(sProductCode + ".jpg");
                Image img = CommonFunctions.byteArrayToImage(arrFileData);
                CommonFunctions.OpenPicViewer(img);
            }
            catch (Exception ex)
            {
                MessageBox.Show("图片不存在！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion

        private void tsmBomDetail_Click(object sender, EventArgs e)
        {
            DataRow dr = gridView1.GetFocusedDataRow();
            if (dr == null)
                return;
            if (string.IsNullOrEmpty(dr["BOM_ID"].ToString()))
            {
                MessageBox.Show("物料清单尚未套用!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            BomProductBomMasterEntity m=ProjMBOMBs.GetProductBomMasterByID(dr["BOM_ID"].ToString());
            frmMaintainMBOM frm = new frmMaintainMBOM();
            frm.ThisActionType = ActionType.View;
            frm.SelectedProductBomMaster = m;
            frm.ShowDialog();
        }
    }
}
